home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ASCIISRC.ZIP / DONITSI.C < prev    next >
C/C++ Source or Header  |  1995-10-14  |  5KB  |  184 lines

  1. #include <stdio.h>
  2. #include <dos.h>
  3. #include <conio.h>
  4. #include <mem.h>
  5. #include <stdlib.h>
  6. char *ctable=" ·-■+o░▒O$&#@▓█";
  7. //char *ctable="   ░░░░▒▒▒▒▓▓▓▓██";
  8. #include "torus.dat"
  9. extern void waittof(void);
  10. #define COSADD (256)
  11.  
  12. #define put_char(x,y,character) \
  13.     ((*(unsigned char far *) MK_FP(0xb800,160*(y)+2*(x)))=(character))
  14. #define put_color(x,y,color) \
  15.     ((*(unsigned char far *) MK_FP(0xb800,160*(y)+2*(x)+1))=(color))
  16.  
  17.  
  18. extern int cos[1025];
  19. extern int sin[1025];
  20. unsigned char buffer[16000];
  21. struct vertex_data_s rotated_vertex_data[VERTEXES];
  22.  
  23. void rotate(int angle,int zoomfactor,int moveplc)
  24. {
  25.     for (int i=0;i<VERTEXES;i++)
  26.     {
  27.         struct vertex_data_s *dest=&rotated_vertex_data[i];
  28.         struct vertex_data_s *src=&vertex_data[i];
  29.         float X=src->Z*(sin[zoomfactor]+32000.0)/16384.0;
  30.         float Y=src->X*(sin[zoomfactor]+32000.0)/16384.0;
  31.         float Z=src->Y*(sin[zoomfactor]+32000.0)/16384.0;
  32.  
  33.         dest->X=(X*sin[(angle+COSADD)&1023]+Z*sin[angle&1023])/32768.0;
  34.         dest->Y=Y/8.0;
  35.         dest->Z=(-X*sin[angle&1023]+Z*sin[(angle+COSADD)&1023])/32768.0;
  36.  
  37.         //X=dest->X;
  38.         //Y=dest->Y;
  39.         //Z=dest->Z;
  40.  
  41.         //dest->X=X;
  42.         //dest->Y=(X*sin[(angle/2+COSADD)&1023]+Y*sin[(angle/2)&1023])/32768.0;
  43.         //dest->Z=(-X*sin[(angle/2)&1023]+Y*sin[(angle/2+COSADD)&1023])/32768.0;
  44.  
  45.         dest->X=dest->X/8.0+40.0+((50.0*sin[moveplc&1023])/16384.0);
  46.         dest->Y=dest->Y/2+25.0+((20.0*sin[(moveplc*2)&1023])/16384.0);
  47.         dest->Z=dest->Z/2;
  48.     }
  49. }
  50. void fline(int x1,int x2,int y,int color)
  51. {
  52.     register int x;
  53.     if (x1>x2) {int temp=x1;x1=x2;x2=temp;}
  54.     if (x1<0) x1=0;
  55.     if (x2>79) x2=79;
  56.     if (x2<0) return;
  57.     if (x1>79) return;
  58.     if (x1>x2) return;
  59.     if (y<0 || y>50) return;
  60.     if (x1==x2) return;
  61.     unsigned *p=//(unsigned far *)MK_FP(0xb800,160*y+x1*2);
  62.         (unsigned *)(buffer+160*y+x1*2);
  63.     register unsigned value=0x1000+ctable[color];
  64.     for (x=x1;x<=x2;x++) {
  65.         *p++=value;
  66.     }
  67. }
  68.  
  69. struct face_data_s sorted_face_data[FACES];
  70.  
  71.  
  72. int compare_face(const void *a,const void *b)
  73. {
  74.     struct face_data_s *A=(struct face_data_s*)a,*B=(struct face_data_s*)b;
  75.     if (rotated_vertex_data[A->A].Z > rotated_vertex_data[B->A].Z)
  76.         return -1;
  77.     else if (rotated_vertex_data[A->A].Z < rotated_vertex_data[B->A].Z)
  78.         return 1;
  79.     return 0;
  80. }
  81.  
  82. void sortfaces(void)
  83. {
  84.     struct face_data_s *src=face_data;
  85.     struct face_data_s *dest=sorted_face_data;
  86.     memcpy(dest,src,sizeof(struct face_data_s)*FACES);
  87.     qsort((void*)dest,FACES,sizeof(struct face_data_s),compare_face);
  88. }
  89.  
  90.  
  91.  
  92. #define min(x,y) ((x)<(y)?(x):(y))
  93. void fillpoly(int facenbr,int color)
  94. {
  95.     int y;
  96.     struct face_data_s *face=&sorted_face_data[facenbr];
  97.     struct vertex_data_s *A=&rotated_vertex_data[face->A];
  98.     struct vertex_data_s *B=&rotated_vertex_data[face->B];
  99.     struct vertex_data_s *C=&rotated_vertex_data[face->C];
  100.  
  101.     struct vertex_data_s E,F;
  102.  
  103.     E.X=A->X - B->X;
  104.     E.Y=A->Y - B->Y;
  105.     //E.Z=A->Z - B->Z;
  106.     F.X=A->X - C->X;
  107.     F.Y=A->Y - C->Y;
  108.     //F.Z=A->Z - C->Z;
  109.  
  110.     //float rx=E.Y*F.Z - E.Z*F.Y;
  111.     //float ry=E.Z*F.X - E.X*F.Z;
  112.     float rz=E.X*F.Y - E.Y*F.X;
  113.  
  114.     if (rz > 0) return;
  115.     color=(int)(-A->Z/16+5)&15;
  116.  
  117.     struct vertex_data_s *AA = (A->Y <= B->Y) ? A : B;
  118.     AA = (AA->Y < C->Y) ? AA : C;
  119.  
  120.     struct vertex_data_s *CC = (A->Y >= B->Y) ? A : B;
  121.     CC = (CC->Y > C->Y) ? CC : C;
  122.  
  123.     if (AA->Y==CC->Y) return;
  124.     struct vertex_data_s *BB=A;
  125.     if ((BB==AA) || (BB==CC)) BB=B;
  126.     if ((BB==AA) || (BB==CC)) BB=C;
  127.  
  128.     int height1=BB->Y - AA->Y;
  129.     int height2=CC->Y - BB->Y;
  130.     int height3=CC->Y - AA->Y;
  131.     int CX1=AA->X*256,CX2=AA->X*256;
  132.     int DX1;
  133.     if (height1==0) DX1=0; else DX1=(BB->X - AA->X)*256/height1/2;
  134.     int DX2;
  135.     if (height3==0) DX2=0; else DX2=(CC->X - AA->X)*256/height3/2;
  136.     for (y=AA->Y; y<=BB->Y; y++)
  137.     {
  138.         fline(CX1/256,CX2/256,y,color);
  139.         CX1+=DX1; CX2+=DX2;
  140.     }
  141.     if (height2==0) DX1=0; else DX1 = (CC->X - BB->X)*256 / height2;
  142.     CX1=BB->X*256;
  143.     for ( ; y<=CC->Y ;y++)
  144.     {
  145.         fline(CX1/256,CX2/256,y,color);
  146.         CX1+=DX1; CX2+=DX2;
  147.     }
  148. }
  149. void rotating_torus(int frame)
  150. {
  151. //    face_data[0].A=0;
  152. //    face_data[0].B=1;
  153. //    face_data[0].C=2;
  154.  //    rotated_vertex_data[0].X=70;
  155.  //    rotated_vertex_data[0].Y=5;
  156.  //    rotated_vertex_data[0].Z=4;
  157.  //    rotated_vertex_data[1].X=72-frame;
  158.  //    rotated_vertex_data[1].Y=19.5;
  159.  //    rotated_vertex_data[1].Z=5;
  160.  //    rotated_vertex_data[2].X=25;
  161.  //    rotated_vertex_data[2].Y=20;
  162.  //    rotated_vertex_data[2].Z=5;
  163. //    rotate(frame);
  164. //    fillpoly(0,5);
  165.  
  166. //    getch();
  167. //    _fmemset(MK_FP(0xb800,0),' ',8000);
  168. //    return;
  169.     int i;
  170.     rotate(frame*15,frame,frame*4);
  171.     sortfaces();
  172.     waittof();
  173.     _fmemcpy(MK_FP(0xb800,0),buffer,8000);
  174.     memset(buffer,' ',8000);
  175.     for (i=0;i<FACES;i++)
  176.     {
  177.         fillpoly(i,31-((i>>3)&31));
  178.     }
  179. }
  180.  
  181.  
  182.  
  183.  
  184.